/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.DomainLayer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author meydlo
 */

public class FileManager {

    private String customers="customers.txt";
    private String workers="workers.txt";

    public void writeWorkersListToFile(ArrayList<Worker> arrayList)
    {
        ObjectOutputStream oos;

        File file=new File(workers);
        if(!file.exists() || !file.canWrite()){
            System.err.println("write: can't save files: " + file.getAbsolutePath());
            return;
        }

        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(arrayList);
            oos.flush();
            oos.close();
        }
        catch(Exception e)
        {
            System.err.println("write: error is: "+e.getMessage());
        }
    }

    public void writeCustomersListToFile(ArrayList<Customer> arrayList)
    {
        ObjectOutputStream oos;

        File file=new File(customers);
        if(!file.exists() || !file.canWrite()){
            System.err.println("write: can't save files: " + file.getAbsolutePath());
            return;
        }

        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(arrayList);
            oos.flush();
            oos.close();
        }
        catch(Exception e)
        {
            System.err.println("write: error is: "+e.getMessage());
        }
    }

    public ArrayList<Worker> readWorkersFromFile()
    {
        ArrayList<Worker> arrayList = null;
        File file=new File(workers);
        if(!file.exists() || !file.canWrite()){
            System.err.println("read: can't save files: " + file.getAbsolutePath());
            return null;
        }

        try{
            FileInputStream fs = new FileInputStream(file);
            ObjectInputStream ois=new ObjectInputStream(fs);
            arrayList=(ArrayList<Worker>) ois.readObject();
            ois.close();
            fs.close();
        }
        catch(Exception e)
        {
            System.err.println("read: error is: "+e.getMessage());
        }
        return arrayList;
    }

    public ArrayList<Customer> readCustomersFromFile()
    {
        ArrayList<Customer> arrayList = null;
        File file=new File(customers);
        if(!file.exists() || !file.canWrite()){
            System.err.println("read: can't save files: " + file.getAbsolutePath());
            return null;
        }

        try{
            FileInputStream fs = new FileInputStream(file);
            ObjectInputStream ois=new ObjectInputStream(fs);
            arrayList=(ArrayList<Customer>) ois.readObject();
            System.out.println(arrayList);
            ois.close();
            fs.close();
        }
        catch(Exception e)
        {
            System.err.println("read: error is: "+e.getMessage());
        }
        return arrayList;
    }

    public void readIT()
    {
        WorkerRecords workerRecords=WorkerRecords.getWorkerRecords();

        workerRecords.setWorkersList(readWorkersFromFile());
    }

    public void writeIT()
    {
        CustomerRecords customerRecords=CustomerRecords.getCustomerRecords();
        WorkerRecords workerRecords= WorkerRecords.getWorkerRecords();

        writeCustomersListToFile(customerRecords.getCustomerList());
        writeWorkersListToFile(workerRecords.getWorkerList());
    }
}
